<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\UserService;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    //用户

    public function __construct(UserService $service)
    {
        $this->db = $service;
    }

    //注册
    public function regist(Request $request)
    {
        $phone = $request->post('phone', '');
        $pwd = $request->post('passwd', '');
        $code = $request->post('code', '');
        if (!$phone || !$pwd || !$code) {
            return $this->toJson(-2, '手机号、密码、验证码必须的');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
            return $this->toJson(-2, '手机号格式错误');
        }
        if (strlen($pwd) < 6 || strlen($pwd) > 20) {
            return $this->toJson(-2, '密码在6到20位之间');
        }
        $res = $this->db->regist($phone, $pwd, $code);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //发送验证码
    public function sendYzm(Request $request)
    {
        $phone = $request->post('phone', '');
        if (!$phone) {
            return $this->toJson(-2, '手机号必须的');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
            return $this->toJson(-2, '手机号格式错误');
        }
        $res = $this->db->yzmGet($phone);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        $sms = new \App\Utilitys\AliSms();
        $resSms = $sms->send($phone, $res['data']['code']);
        if ($resSms['code'] < 0) {
            return $this->toJson(-2, $resSms['msg']);
        }
        $this->db->yzmModify($res['data']['id']);
        return $this->toJson(0, '', $res['data']);
    }

    //登录
    public function dologin(Request $request)
    {
        $phone = $request->post('phone', '');
        $pwd = $request->post('passwd', '');
        if (!$phone || !$pwd) {
            return $this->toJson(-2, '手机号、密码必须的');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
            return $this->toJson(-2, '手机号格式错误');
        }
        $res = $this->db->dologin($phone, $pwd);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //通过验证码登录
    public function loginByYzm(Request $request)
    {
        $phone = $request->post('phone', '');
        $yzm = $request->post('yzm', '');
        if (!$phone || !$yzm) {
            return $this->toJson(-2, '手机号、验证码必须的');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
            return $this->toJson(-2, '手机号格式错误');
        }
        $res = $this->db->loginByYzm($phone, $yzm);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //注销
    public function logoff(Request $request)
    {
        $token = $request->post('token', '');
        $res = $this->db->logoff($token);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }

    //实名认证申请
    public function authReal(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        $pas['realname'] = $request->post('realname', '');
        $pas['sex'] = $request->post('sex', 0); //1男，2女
        $pas['phone'] = $request->post('phone', '');
        $pas['id_card'] = $request->post('id_card', '');
        $pas['address_now'] = $request->post('address_now', '');
        $pas['zfb_account'] = $request->post('zfb_account', '');
        $pas['wx_account'] = $request->post('wx_account', '');
        $pas['id_front'] = $request->post('id_front', '');
        $pas['id_back'] = $request->post('id_back', '');
        if (!$pas['realname']) {
            return $this->toJson(-2, '姓名必须的');
        }
        if (!in_array($pas['sex'], [1, 2])) {
            return $this->toJson(-2, '性别错误');
        }
        if (!preg_match('/^1[3-9]\d{9}$/', $pas['phone'])) {
            return $this->toJson(-2, '手机号格式错误');
        }
        if (!$pas['address_now']) {
            return $this->toJson(-2, '现住址必须的');
        }
        if (!$pas['id_card']) {
            return $this->toJson(-2, '身份证号码必须的');
        }
        if (!$pas['id_front'] || !$pas['id_back']) {
            return $this->toJson(-2, '身份证前后照必须的');
        }
        $res = $this->db->authReal($uid, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //通过token，获取用户信息
    public function infoByToken(Request $request)
    {
        $token = $request->post('token', '');
        $res = $this->db->infoByToken($token);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //通过UID，获取用户信息
    public function infoByUid(Request $request)
    {
        $uid = $request->post('uid', '');
        $res = $this->db->infoByUid($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //关注设置
    public function setFollow(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $blogUid = $request->post('blogUid', 0); //博主UID
        $res = $this->db->setFollow($uid, $blogUid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //健康专栏
    public function healthyList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->healthyList($uid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //健康专栏详情
    public function healthyOne(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->healthyOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 提现列表
     */
    public function tixianList(Request $request)
    {
        $token = $request->post('token', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->tixianList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 提现申请
     */
    public function tixianApply(Request $request)
    {
        $token = $request->post('token', '');
        $pas['pay_way'] = $request->post('pay_way', 0); //1微信，2支付宝
        $pas['money'] = $request->post('money', 0);
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $uOne = $this->db->infoByUid($pas['uid']);
        if ($uOne['code']<0) {
            return $this->toJson(-2, '没有登录');
        }
        $info=$uOne['data'];
        if($info['balance']<$pas['money'])
        {
            return $this->toJson(-2, '余额不足');
        }
        $pas['money'] = $pas['money'] ? $pas['money'] * 100 : 0;
        $pas['fee']   = $pas['money'] * sys_config('withdrawal_fee',0);
        $pas['withdraw'] = $pas['money'] - $pas['fee'] ;
        $res = $this->db->tixianApply($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
    public function getTxConfig()
    {
        $data['fee']=sys_config('withdrawal_fee',0);
        $data['note']=sys_config('withdrawal_note','');
        return $this->toJson(0 ,'success', $data);
    }
}